//
//  HWTradingViewController.m
//  HWanGame
//
//  Created by Chendaobin on 2019/8/23.
//  Copyright © 2019年 Chendaobin. All rights reserved.
//   https://lanhuapp.com/url/ioAHf-rGzm5 

#import "HWTradingViewController.h"
#import "HWClinchDealCell.h"
#import "HWChenjiaoDonTaiViewController.h"
#import "HWScreeningView.h" //筛选View
#import "HWSortingView.h"
#import "HWQuanBuView.h"
#import "HWJiaoyixuzhiViewController.h"
#import "HWAccountDetailsViewController.h"
#import "HWViewSellNoController.h" // 我要卖号
#import "HWCustomerServiceCenterViewController.h" // 客服中心
#import "HWTradingModel.h"
#import "HWsXiaoHaoGuanLiViewController.h"
#import "HWLoginController.h" // 登录界面
#import "HWMyTradingViewController.h" //我的交易
#import "HWTradecollectionShouCangViewController.h" // 我的收藏




@interface HWTradingViewController ()<UITableViewDelegate,UITableViewDataSource,HWQuanBuViewDelegate,HWScreeningViewDelegate,HWSortingViewDelegate>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSArray *dataArr;
@property(strong,nonatomic) NSArray *icoImageArr;
@property(strong,nonatomic) UIView *sectionheaderView;
@property(strong,nonatomic) HWScreeningView *screeningView;
@property(strong,nonatomic) HWSortingView *sortingView;
@property(strong,nonatomic) HWQuanBuView *quanBuView;
@property(strong,nonatomic) NSMutableArray *fenLeiMuArr;

@property(strong,nonatomic) NSMutableArray *shujudataMuArr;
@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页
@property(strong,nonatomic) NSString *pandunSr;

@property(strong,nonatomic) NSString *sort;  // 默认 0 按添加顺序 1 价格 高->低 ，2 价格 低->高
@property(strong,nonatomic) NSString *system; // 0 双端 1 安卓 2 IOS
@property(strong,nonatomic) NSString *smoney; // 开始金额
@property(strong,nonatomic) NSString *emoney; // 结束金额
@property(strong,nonatomic) NSString *keywordstr;  // 关键词搜索





@end

@implementation HWTradingViewController

-(HWScreeningView *)screeningView {
    
    if (_screeningView == nil) {
        _screeningView = [[HWScreeningView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        _screeningView.backgroundColor  = [UIColor  clearColor];
        
    }
    
    
    return  _screeningView;
    
    
}


-(HWSortingView *)sortingView  {
    if (_sortingView ==nil) {
        _sortingView = [[HWSortingView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        _sortingView.backgroundColor  = [UIColor  clearColor];
        
    }
    
    return _sortingView;
    
}

-(HWQuanBuView *)quanBuView {
    
    if (_quanBuView == nil) {
        _quanBuView  = [[HWQuanBuView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
        _quanBuView.backgroundColor  = [UIColor  clearColor];
        
    }

    return _quanBuView;
    
    
}

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
    
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-CF_TabbarHeight) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        
        
        
    }
    return   _mainaTableVie ;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    // 设置UI
    [self setupUI];
    
    // 设置tabView的header
    [self addheaderView];
    // 设置常用的属性
    [self setCommonPropertiesShuXing];
    [self addrefreshControl];
    
}


// 设置常用的属性
- (void)setCommonPropertiesShuXing {
//     @[@"全部分类",@"筛选",@"排序"];
    self.fenLeiMuArr = [[NSMutableArray alloc]init];
    [self.fenLeiMuArr addObject:@"全部分类"];
    [self.fenLeiMuArr addObject:@"筛选"];
    [self.fenLeiMuArr addObject:@"排序"];
    
    self.sort = @"0";
    self.system= @"0";
    self.smoney = @"";
    self.emoney = @"";
    self.keywordstr = @"";
    
    // 卖家须知
    
}



#pragma  mark ---- 添加刷新

- (void)addrefreshControl {

    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];

    [self.mainaTableVie.mj_header beginRefreshing];

    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {



    self.p = @"1";

    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];

   
    self.shujudataMuArr = [[NSMutableArray alloc]init];
    
//
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"trade",
                                       @"sort" : self.sort,
                                       @"system": self.system,
                                       @"smoney" : self.smoney,
                                       @"emoney" : self.emoney,
                                       @"keyword" :self.keywordstr,
                                       };
    
    
  

       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {

           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];


           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

           if ([codeStr intValue] == 1) {

              self.shujudataMuArr = [HWTradingModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];

             


           } else if ([codeStr intValue] == 0) {
               
               
               
               
           }
           [self.mainaTableVie reloadData];


       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];


       }];


}

// 上拉加载

- (void)onRefresh {



    int p = [self.p intValue];

    int max_p = [self.max_p intValue];

     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
                            
        return ;

      }


    NSDictionary *parametes = @{
                                    @"p" :self.p,
                                     @"ios" : @"1",
                                     @"ct" :  @"app",
                                     @"ac" : @"trade",
                                      @"sort" :self.sort,
                                      @"system": self.system,
                                      @"smoney" : self.smoney,
                                      @"emoney" : self.emoney,
                                     @"keyword" :self.keywordstr,
                                            };
         
    

        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {

             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];


             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

             if ([codeStr intValue] == 1) {




                 NSMutableArray *mutableArr = [HWTradingModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.shujudataMuArr addObject:mutableArr[i]];

                 }

                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.mainaTableVie reloadData];


             }
            [self.mainaTableVie reloadData];


         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];


         }];




}






- (void)setupUI {
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"deal_ntradingxuzhi"] style:UIBarButtonItemStylePlain target:self action:@selector(jiaoyixuzhi)];
    
    
     self.navigationItem.title = @"账号交易";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainaTableVie];
    
    

}


// 交易须知
- (void)jiaoyixuzhi {
    
    HWJiaoyixuzhiViewController *jiaoyi = [[HWJiaoyixuzhiViewController alloc]init];
    [self.navigationController pushViewController:jiaoyi animated:YES];
    
    
}







// 添加tabviewheader
- (void)addheaderView {
    UIView *hederview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 92)];
    hederview.backgroundColor = [UIColor whiteColor];
    self.mainaTableVie.tableHeaderView = hederview;
    
    UIView *zhanghaojiaoyiView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 92)];
 //   zhanghaojiaoyiView.backgroundColor = [UIColor redColor];
    [hederview addSubview:zhanghaojiaoyiView];
    
    
    self.dataArr = @[@"成交动态",@"我要卖号",@"我的交易",@"交易收藏"];
    self.icoImageArr = @[@"deal_d",@"deal_soutmaihao",@"deal_minemao",@"deal_s"];
    
    CGFloat w =  (SCREEN_WIDTH- 60*4)/8;
 
    for (int i = 0; i < self.dataArr.count; i++) {
        int numshu = (i+1)*2-1;
        UIView *redview =  [[UIView alloc]initWithFrame:CGRectMake(w*numshu+i*60, 12, 60, 67)];
      //  redview.backgroundColor = [UIColor orangeColor];
        
        [zhanghaojiaoyiView addSubview:redview];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 40, 40)];
        imageView.image  = [UIImage imageNamed:self.icoImageArr[i]];
        imageView.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tap];
        
        
        [redview addSubview:imageView];
        
        UILabel *texelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 40+15, 60, 12)];
        texelabel.textAlignment = NSTextAlignmentCenter;
        texelabel.textColor = RGB(51, 51, 51);
        texelabel.text = self.dataArr[i];
        texelabel.font = [UIFont systemFontOfSize:12];
        [redview addSubview:texelabel];
       
    
        
        
    }
    
    
    
    
    
    
    
    
    
    
}

#pragma  mark ------    跳转

- (void)tapView:(UITapGestureRecognizer *) sende {
    
    UIView *view = sende.view;
    if (view.tag == 0) {
        HWChenjiaoDonTaiViewController *chengjia = [[HWChenjiaoDonTaiViewController alloc]init];
        [self.navigationController pushViewController:chengjia animated:YES];

             
                  
        
        
        
    } else if (view.tag == 1) {
        
//        HWViewSellNoController *SellNo = [[HWViewSellNoController alloc]init];
//        [self.navigationController pushViewController:SellNo animated:YES];
        
        
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
            HWLoginController  *login = [[HWLoginController alloc]init];
                  [self.navigationController pushViewController:login animated:YES];
                 return;
          
        }
        
        HWsXiaoHaoGuanLiViewController *xiaoHao = [[HWsXiaoHaoGuanLiViewController alloc]init];
        [self.navigationController pushViewController:xiaoHao animated:YES];
               
        
        
        
    } else if (view.tag == 2) {
        
        
        
        HWMyTradingViewController *myTrading  = [[HWMyTradingViewController alloc]init];
        [self.navigationController pushViewController:myTrading animated:YES];
        

        
    } else {
        
//        HWCustomerServiceCenterViewController *customerService = [[HWCustomerServiceCenterViewController alloc]init];
//        [self.navigationController pushViewController:customerService animated:YES];
        if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
                  HWLoginController  *login = [[HWLoginController alloc]init];
                        [self.navigationController pushViewController:login animated:YES];
                       return;
                
       }
        
        HWTradecollectionShouCangViewController *collection = [[HWTradecollectionShouCangViewController alloc]init];
        [self.navigationController pushViewController:collection animated:YES];
        
        
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.shujudataMuArr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    HWClinchDealCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWClinchDealCell" owner:nil options:nil].lastObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    cell.model =  self.shujudataMuArr[indexPath.row];
    
    return  cell;
    

}


#pragma  mark -------------- UITableViewDelegate


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return  86;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return  36;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return  0.001;
    
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 36)];
   // view.backgroundColor = [UIColor redColor];
    self.sectionheaderView = view;
    view.backgroundColor = [UIColor whiteColor];
    
   
    NSArray *icoArr = @[@"downIco",@"screeningIco",@"downIco"];
    
    for (int i = 0; i < self.fenLeiMuArr.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:self.fenLeiMuArr[i]  forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:RGB(50, 50, 50) forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:icoArr[i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
      //  button.backgroundColor = [UIColor yellowColor];

       // [button  layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:10];
        
        
        if (i == 0) {
            button.frame =CGRectMake(12, 5, 71, 26);
           [button  layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
            
            
        } else if (i == 1) {
            button.frame =CGRectMake(12+71+9, 5, 71, 26);
            [button  layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
            
        }else {
            
            
            button.frame =CGRectMake(SCREEN_WIDTH-71-12, 5, 71, 26);
            [button  layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:12];
        }
        
        
        [view addSubview:button];
        
    }
    
    return view;
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    HWTradingModel   *model =   self.shujudataMuArr[indexPath.row];
    HWAccountDetailsViewController *accountDetails = [[HWAccountDetailsViewController alloc]init];
    accountDetails.idStr = model.id;
    [self.navigationController pushViewController:accountDetails animated:YES];


}



#pragma mark ---------   点击Button按钮
- (void)buttonTap:(UIButton *)sender {
    
    NSInteger tap = sender.tag;
    CGPoint  redCenterInView = [self.mainaTableVie convertPoint:self.sectionheaderView.center toView:self.view];
    
    
    
    
    NSLog(@"打印 %@",NSStringFromCGPoint(redCenterInView));
    
    
    if (tap == 0) {
        DLog(@"第一个button");
        [kKeyWindow addSubview:self.quanBuView];
        self.quanBuView.delagete = self;
        self.quanBuView.whibackvieY =  redCenterInView.y + 18;
        [self.quanBuView relayoutTheChildControls];
        
        DLog(@"第二个button");
        
        
    } else if (tap == 1) {

        
        [kKeyWindow addSubview:self.screeningView];
        self.screeningView.whibackvieY = redCenterInView.y + 18;
        self.screeningView.delegate = self;
        [self.screeningView relayoutTheChildControls];

         DLog(@"第二个button");
       //  [self ]
       // _quanBuView
        
    }else {
        
        [kKeyWindow addSubview:self.sortingView];
        self.sortingView.whibackvieY =  redCenterInView.y + 18;
        self.sortingView.delagate = self;
        
        [self.sortingView relayoutTheChildControls];
        
         DLog(@"第二个button");
        
    }
    
    
    
}


#pragma mark ---------  HWQuanBuViewDelegate
// 0 代表全部 1 。iOS端  2 安桌端

- (void)HWQuanBuViewXuantap:(int) tap {
    
    if (tap == 0) {
        
      
          self.fenLeiMuArr = [[NSMutableArray alloc]init];
          [self.fenLeiMuArr addObject:@"全部分类"];
          [self.fenLeiMuArr addObject:@"筛选"];
          [self.fenLeiMuArr addObject:@"排序"];
        
           self.system = @"0";
         [self.mainaTableVie.mj_header beginRefreshing];
        
        
    } else if (tap == 1) {
        
        self.fenLeiMuArr = [[NSMutableArray alloc]init];
         [self.fenLeiMuArr addObject:@"iOS端 "];
         [self.fenLeiMuArr addObject:@"筛选"];
         [self.fenLeiMuArr addObject:@"排序"];
          self.system = @"2";
         [self.mainaTableVie.mj_header beginRefreshing];
        
        
    } else {
        
         self.fenLeiMuArr = [[NSMutableArray alloc]init];
         [self.fenLeiMuArr addObject:@"安桌端"];
         [self.fenLeiMuArr addObject:@"筛选"];
         [self.fenLeiMuArr addObject:@"排序"];
         self.system = @"1";
         [self.mainaTableVie.mj_header beginRefreshing];
        
    }
    
    
    
}



//
- (void)HWScreeningViewDic:(NSDictionary *)dict {
//     NSDictionary *dic = @{@"keysub":self.nameTextField.text,@"zuidiStr":zuidiStr,@"zuigaoStr":zuigaoStr};
    
    self.keywordstr = dict[@"keysub"];
    self.smoney = dict[@"zuidiStr"];
    self.emoney  = dict[@"zuigaoStr"];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
}



- (void)HWSortingViewXuZhong:(int )tag {
    
    if (tag == 0) {
        self.sort = @"0";
         
    } else if (tag == 1) {
         self.sort = @"1";
        
    } else if (tag == 2) {
        
         self.sort = @"2";
        
       //  [self.mainaTableVie.mj_header beginRefreshing];
    }
    
     [self.mainaTableVie.mj_header beginRefreshing];
    
}


@end
